//
//  YMFCollectionViewCell.swift
//  FlyingPigeon
//
//  Created by yang on 2019/5/26.
//  Copyright © 2019年 a. All rights reserved.
//

import Foundation
import UIKit


class YMFPriceModel: NSObject, NSCoding {
    
    var showImage = ""
    var headerImage = ""
    var name = ""
    var price = ""
    var oldPrice = ""
    
    var placeName = ""
    
    
    init(dict: [String: String]) {
        super.init()
        
        showImage = dict["showImage"] ?? ""
        headerImage = dict["headerImage"] ?? ""
        name = dict["name"] ?? ""
        price = dict["price"] ?? ""
        oldPrice = dict["oldPrice"] ?? ""
        
        placeName = dict["placeName"] ?? ""
        
    }
    
    override init() {
        super.init()
    }
    
    override func setValue(_ value: Any?, forUndefinedKey key: String) {
        print("\(key)")
    }
    
    
    func encode(with aCoder: NSCoder) {
        aCoder.encode(showImage, forKey: "showImage")
        aCoder.encode(headerImage, forKey: "headerImage")
        aCoder.encode(name, forKey: "name")
        aCoder.encode(price, forKey: "price")
        aCoder.encode(oldPrice, forKey: "oldPrice")
         aCoder.encode(placeName, forKey: "placeName")
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        showImage = aDecoder.decodeObject(forKey: "showImage") as? String ?? ""
        headerImage = aDecoder.decodeObject(forKey: "headerImage") as? String ?? ""
        name = aDecoder.decodeObject(forKey: "name") as? String ?? ""
        price = aDecoder.decodeObject(forKey: "price") as? String ?? ""
        oldPrice = aDecoder.decodeObject(forKey: "oldPrice") as? String ?? ""
        placeName = aDecoder.decodeObject(forKey: "placeName") as? String ?? ""
        
    }
    

}


class YMFCollectionViewCell: UICollectionViewCell {
    
    @IBOutlet weak var backgrounView: UIView!
    @IBOutlet weak var headerImageView: UIImageView!
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var priceLabel: UILabel!
    @IBOutlet weak var oldPriceLabel: UILabel!
    
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        backgroundView?.layer.cornerRadius = 10
        backgroundView?.clipsToBounds = true
    }
    
    func updateWithModel(model: YMFPriceModel) {
        headerImageView.image = UIImage(named: model.headerImage)
        nameLabel.text = model.name
        
        let attrStr = NSMutableAttributedString(string: "￥" + model.price + "/天")
        attrStr.addAttributes([NSAttributedStringKey.foregroundColor : UIColor.red], range: NSRange(location: 1, length: model.price.count))
        priceLabel.attributedText = attrStr
        
        let oldAttrStr = NSAttributedString(string: "￥" + model.oldPrice + "/天", attributes: [NSAttributedStringKey.strikethroughStyle : 1, NSAttributedStringKey.foregroundColor : UIColor.lightGray])
        oldPriceLabel.attributedText = oldAttrStr
        
    }
}
